﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Query.SystemModule.ViewObject;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.Core.Domain.Model;
using Enterprise.Framework.DomainModel.Specifications;
using Enterprise.Framework.DomainModel;
using Resto.CostControl.Core.Cache;

namespace Resto.CostControl.Core.Query.SystemModule.QueryObject
{
    public class DictionaryQO : IApplicationQueryObject
    {
        private IQueryable<DictionaryVO> GetDictionaryVOSet()
        {
            IEntityRepository<Dictionary> repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<Dictionary>();

            IQueryable<DictionaryVO> list = from o in repository.Objects                                            
                                            orderby o.TypeCode,o.Sequence
                                            select new DictionaryVO()
                                            {
                                                ID = o.ID,
                                                Code = o.Code,
                                                Name = o.Name,
                                                Description = o.Description,
                                                PingYinCode = o.PingYinCode,
                                                Sequence = o.Sequence,
                                                TypeCode = o.TypeCode,
                                            };

            return list;
        }

        public DictionaryVO[] QueryDictionaryVOByPager(ISpecification<DictionaryVO> spec, PagerCondition pager, string orderKey, bool isAscending)
        {
            IQueryable<DictionaryVO> list = GetDictionaryVOSet();

            if (spec != null)
                list = list.Where(spec.GetExpression());

            IEnumerable<DictionaryVO> list2 = RepositoryExtention.Query(list, pager, orderKey, isAscending);
            return list2.ToArray();
        }

        internal DictionaryVO[] GetDictionaryArrayByType(string typeCode)
        {
            IQueryable<DictionaryVO> list = GetDictionaryVOSet();
            var list2 = from o in list
                        where o.TypeCode == typeCode
                        orderby o.Sequence,o.Name
                        select o;

            return list2.ToArray();

        }

    }
}
